import { reactive } from "vue";
import apiPage from "@/api/page";

let authorityRead = reactive<any>(false);
let authorityManage = reactive<any>(false);

async function roleInit() {
  const userInfo = localStorage.getItem("userInfo");
  if (userInfo) {
    const userId = JSON.parse(userInfo)?.id;

    const roleList: any = await apiPage.getRoleList({
      roleId: userId,
      resourceMarks: ["big-screen-page"],
    });
    if (roleList.code == 2000010000) {
      for (let i = 0; i < roleList.data.length; i++) {
        if (roleList.data[i].resourceOperation === "read") {
          authorityRead = true;
        } else if (roleList.data[i].resourceOperation === "manage") {
          authorityManage = true;
        }
      }
      return { authorityRead, authorityManage };
    }
  }
}

function getRole() {
  return { authorityRead, authorityManage };
}

export default { roleInit, getRole };
export { roleInit, getRole };
